@echo off
REM Copy built dynamic libraries to the correct location

echo Setting up library files...

REM 检查源目录是否存在
if not exist "onnxruntime\build" (
    echo 错误: 未找到onnxruntime\build目录，请先运行build_onnxruntime.bat构建库文件
    exit /b 1
)

REM 创建目标目录
echo 创建目标目录...
if not exist "build\windows\x64\runner\Debug\" mkdir "build\windows\x64\runner\Debug\"
if not exist "build\windows\x64\runner\Release\" mkdir "build\windows\x64\runner\Release\"

REM 尝试从Debug目录复制
set "SOURCE_DIR=onnxruntime\build\bin\Debug"
set "SOURCE_DIR_RELEASE=onnxruntime\build\bin\Release"
set "TARGET_DEBUG_DIR=build\windows\x64\runner\Debug"
set "TARGET_RELEASE_DIR=build\windows\x64\runner\Release"

echo 正在复制库文件...

REM 复制Debug版本
if exist "%SOURCE_DIR%\funasr_flutter.dll" (
    echo 复制Debug版本: %SOURCE_DIR%\funasr_flutter.dll
    copy "%SOURCE_DIR%\funasr_flutter.dll" "%TARGET_DEBUG_DIR%\"
    
    REM 同时复制Debug版本到Release目录，以防Release版本不存在
    copy "%SOURCE_DIR%\funasr_flutter.dll" "%TARGET_RELEASE_DIR%\"
) else (
    echo 未找到Debug版本库文件: %SOURCE_DIR%\funasr_flutter.dll
)

REM 复制Release版本（如果存在）
if exist "%SOURCE_DIR_RELEASE%\funasr_flutter.dll" (
    echo 复制Release版本: %SOURCE_DIR_RELEASE%\funasr_flutter.dll
    copy "%SOURCE_DIR_RELEASE%\funasr_flutter.dll" "%TARGET_DEBUG_DIR%\"
    copy "%SOURCE_DIR_RELEASE%\funasr_flutter.dll" "%TARGET_RELEASE_DIR%\"
) else (
    echo 未找到Release版本库文件: %SOURCE_DIR_RELEASE%\funasr_flutter.dll
)

REM 检查是否至少复制了一个版本
if not exist "%TARGET_DEBUG_DIR%\funasr_flutter.dll" (
    if not exist "%TARGET_RELEASE_DIR%\funasr_flutter.dll" (
        echo 错误: 未找到任何版本的funasr_flutter.dll，请确保库已成功构建
        exit /b 1
    )
)

REM 复制依赖库文件（如果存在）
echo 正在复制依赖库文件...
set "ONNXRUNTIME_LIB_DIR=onnxruntime\onnxruntime-win-x64-1.16.1\lib"
set "FFMPEG_LIB_DIR=onnxruntime\ffmpeg-master-latest-win64-gpl-shared\bin"

if exist "%ONNXRUNTIME_LIB_DIR%" (
    echo 复制onnxruntime依赖库...
    xcopy "%ONNXRUNTIME_LIB_DIR%\*.dll" "%TARGET_DEBUG_DIR%\" /Y >nul
    xcopy "%ONNXRUNTIME_LIB_DIR%\*.dll" "%TARGET_RELEASE_DIR%\" /Y >nul
)

if exist "%FFMPEG_LIB_DIR%" (
    echo 复制ffmpeg依赖库...
    xcopy "%FFMPEG_LIB_DIR%\*.dll" "%TARGET_DEBUG_DIR%\" /Y >nul
    xcopy "%FFMPEG_LIB_DIR%\*.dll" "%TARGET_RELEASE_DIR%\" /Y >nul
)

echo.
echo 库文件部署完成！
echo 请确保以下依赖库也已正确部署:
echo 1. onnxruntime.dll
echo 2. ffmpeg相关dll文件 (如avcodec-58.dll, avformat-58.dll等)
echo.
echo 如果运行时出现"找不到指定模块"错误，请检查依赖库是否完整